package com.ruicar.afs.cloud.afscase.loanmoderuleinfo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.loanmoderuleinfo.entity.LoanModeRuleInfo;
import com.ruicar.afs.cloud.afscase.loanmoderuleinfo.mapper.LoanModeRuleInfoMapper;
import com.ruicar.afs.cloud.afscase.loanmoderuleinfo.service.LoanModeRuleInfoService;
import com.ruicar.afs.cloud.bizcommon.rules.entity.AfsRuleInfo;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author panhj
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class LoanModeRuleInfoServiceImpl  extends ServiceImpl<LoanModeRuleInfoMapper, LoanModeRuleInfo> implements LoanModeRuleInfoService {
    private AfsRuleInfoService afsRuleInfoService;
    /**
     * @param
     * @description 停用规则
     * @author panhj
     * @date 2020/8/28
     */
    @Override
    public void deActiveRule(Long id) throws ParseException {
        //设为停用
        Date da = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date2 = format.format(da);
        LoanModeRuleInfo loanModeRuleInfo = this.getById(id);
            if (null != loanModeRuleInfo) {
                loanModeRuleInfo.setIsEnable(WhetherEnum.NO.getCode());
                loanModeRuleInfo.setFailureTime(format.parse(date2));
                this.updateById(loanModeRuleInfo);
                //设置规则失效
                AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanModeRuleInfo.getRuleId());
                if (null != afsRuleInfo) {
                    afsRuleInfoService.deActiveRule(afsRuleInfo.getId());
                }
            }
    }

    /**
     * @param
     * @description 启用规则
     * @author panhj
     * @date 2020/8/28
     */
    @Override
    public void activeRule(Long id) throws ParseException {
        //设为启用
        Date da = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date2 = format.format(da);
            LoanModeRuleInfo loanModeRuleInfo = this.getById(id);
            if (null != loanModeRuleInfo) {
                loanModeRuleInfo.setIsEnable(WhetherEnum.YES.getCode());
                loanModeRuleInfo.setEffectTime(format.parse(date2));
                this.updateById(loanModeRuleInfo);
                //激活规则
                AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanModeRuleInfo.getRuleId());
                if (null != afsRuleInfo) {
                    afsRuleInfoService.activeRule(afsRuleInfo.getId());
                }
            }
    }

    /**
     * @param
     * @description 更新同步状态 同步时间
     * @author panhj
     * @date 2020/8/29
     */
    @Override
    public void updateGpsRulesInfo(Long id) {
        this.update(Wrappers.<LoanModeRuleInfo>lambdaUpdate()
                .eq(LoanModeRuleInfo::getId, id)
                .set(LoanModeRuleInfo::getIsSynchronous, WhetherEnum.YES.getCode())
                .set(LoanModeRuleInfo::getSynchronousTime, new Date()));
    }
}
